<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录</title>

    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/login.css">
    <!-- 添加字体图标库 -->
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css" rel="stylesheet">
    <style>
        .nav {
            background-color: rgba(255, 255, 255, 0.9);
            padding: 0px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            font-size: 24px;
            font-weight: bold;
            color: #333;
        }

        .login-container {
            display: flex;
            justify-content: center;
            align-items: center;
            height: calc(100vh - 60px);
        }

        .login-dialog {
            background: rgba(255, 255, 255, 0.6);
            padding: 30px 40px;
            border-radius: 15px;
            box-shadow: 0 8px 20px rgba(0, 0, 0, 0.1);
            width: 400px;
            transition: transform 0.3s ease;
        }

        .login-dialog:hover {
            transform: translateY(-5px);
        }

        h3 {
            text-align: center;
            color: #333;
            margin-bottom: 15px;
            font-size: 24px;
        }

        .row {
            margin-bottom: 15px;
            position: relative;
        }

        .row span {
            display: block;
            margin-bottom: 5px;
            color: #666;
            font-size: 14px;
        }

        input {
            width: 100%;
            padding: 10px 12px 10px 45px;
            border: 1px solid #ddd;
            border-radius: 8px;
            outline: none;
            transition: border-color 0.3s ease;
            box-sizing: border-box;
            font-size: 14px;
        }

        input:focus {
            border-color: #a1c4fd;
            box-shadow: 0 0 5px rgba(161, 196, 253, 0.5);
        }

        button {
            width: 100%;
            padding: 10px;
            background: linear-gradient(120deg, #a1c4fd 0%, #c2e9fb 100%);
            border: none;
            border-radius: 8px;
            color: white;
            font-size: 16px;
            cursor: pointer;
            transition: transform 0.2s ease, box-shadow 0.2s ease;
        }

        button:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(161, 196, 253, 0.4);
        }

        button:active {
            transform: translateY(0);
        }

        .row i {
            position: absolute;
            left: 30px;
            top: 33px;
            color: #666;
        }

        input::placeholder {
            color: #999;
        }

        .switch-form {
            text-align: center;
            margin-top: 12px;
            font-size: 14px;
            color: #666;
        }

        .switch-form a {
            color: #a1c4fd;
            text-decoration: none;
            cursor: pointer;
        }

        .switch-form a:hover {
            text-decoration: underline;
        }

        .hidden {
            display: none;
        }
    </style>
</head>
<body>
<div class="nav">
    弈江湖五子棋在线对战平台
</div>
<div class="login-container">
    <div class="login-dialog">
        <!-- 登录表单 -->
        <div id="loginForm">
            <h3>用户登录</h3>
            <div class="row">
                <span>账号</span>
                <i class="fas fa-user"></i>
                <input type="text" id="username" placeholder="请输入用户名">
            </div>
            <div class="row">
                <span>密码</span>
                <i class="fas fa-lock"></i>
                <input type="password" id="password" placeholder="请输入密码">
            </div>
            <div class="row">
                <button id="loginSubmit">登 录</button>
            </div>
            <div class="switch-form">
                还没有账号？<a id="showRegister">立即注册</a>
            </div>
        </div>

        <!-- 注册表单 -->
        <div id="registerForm" class="hidden">
            <h3>用户注册</h3>
            <div class="row">
                <span>账号</span>
                <input type="text" id="registerUsername" placeholder="请输入用户名">
            </div>
            <div class="row">
                <span>密码</span>
                <input type="password" id="registerPassword" placeholder="请输入密码">
            </div>
            <div class="row">
                <button id="registerSubmit">注 册</button>
            </div>
            <div class="switch-form">
                已有账号？<a id="showLogin">立即登录</a>
            </div>
        </div>
    </div>
</div>

<script src="./js/jquery.min.js"></script>
<script>
    // 表单切换逻辑
    const loginForm = document.querySelector('#loginForm');
    const registerForm = document.querySelector('#registerForm');
    const showRegister = document.querySelector('#showRegister');
    const showLogin = document.querySelector('#showLogin');

    showRegister.onclick = function() {
        loginForm.classList.add('hidden');
        registerForm.classList.remove('hidden');
    }

    showLogin.onclick = function() {
        registerForm.classList.add('hidden');
        loginForm.classList.remove('hidden');
    }

    // 登录逻辑
    let loginSubmit = document.querySelector('#loginSubmit');
    loginSubmit.onclick = function() {
        $.ajax({
            type: 'post',
            url: '/login',
            data: {
                username: document.querySelector('#username').value,
                password: document.querySelector('#password').value,
            },
            success: function(body) {
                if (body && body.userId > 0) {
                    alert("登录成功!");
                    location.assign('/game_hall.html');
                } else {
                    alert("登录失败!");
                }
            },
            error: function() {
                alert("登录失败!");
            }
        });
    }

    // 注册逻辑
    let registerSubmit = document.querySelector('#registerSubmit');
    registerSubmit.onclick = function() {
        $.ajax({
            type: 'post',
            url: '/register',
            data: {
                username: document.querySelector('#registerUsername').value,
                password: document.querySelector('#registerPassword').value,
            },
            success: function(body) {
                if (body && body.username) {
                    alert("注册成功!");
                    registerForm.classList.add('hidden');
                    loginForm.classList.remove('hidden');
                } else {
                    alert("注册失败!");
                }
            },
            error: function() {
                alert("注册失败!");
            }
        });
    }
</script>
</body>
</html>